/*
 *    Copyright 2009 Simone Tripodi
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.memos2;

import net.spy.memcached.ConnectionFactory;
import net.spy.memcached.DefaultConnectionFactory;

/**
 * Setter from String to ConnectionFactory representation.
 *
 * @author Simone Tripodi (simone.tripodi)
 * @version $Id: ConnectionFactorySetter.java 94 2009-12-16 09:46:27Z simone.tripodi $
 */
final class ConnectionFactorySetter extends AbstractPropertySetter<ConnectionFactory> {

    /**
     * Instantiates a String to ConnectionFactory setter.
     */
    public ConnectionFactorySetter() {
        super("cache.memos2.connectionfactory",
                "connectionFactory",
                new DefaultConnectionFactory());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected ConnectionFactory convert(String property) throws Throwable {
        Class<?> clazz = Class.forName(property);
        if (!ConnectionFactory.class.isAssignableFrom(clazz)) {
            throw new IllegalArgumentException("Class '"
                    + clazz.getName()
                    + "' is not a valid '"
                    + ConnectionFactory.class.getName()
                    + "' implementation");
        }
        return (ConnectionFactory) clazz.newInstance();
    }

}
